package com.his.modules.sysjob.job;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.core.utils.HttpUtils;
import com.his.core.utils.StringUtils;
import com.his.modules.business.entity.CaSignDataLis;
import com.his.modules.business.service.CaSignDataLisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Component("caSignDataQmnrAndCertLisJob")
@Slf4j
public class CaSignDataQmnrAndCertLisJob {
    @Autowired
    private CaSignDataLisService caSignDataService;
    /**
     * CA获取签名值、用户证书接口
     * @return
     */
    public void caSignDataQmnrAndCertLisJob() {
        log.info("CA获取Lis签名值、用户证书接口定时任务开始执行=================");
        QueryWrapper<CaSignDataLis> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("QMNR");

        List<CaSignDataLis> caSignDataList = caSignDataService.list(queryWrapper);
        if(!CollectionUtils.isEmpty(caSignDataList)) {
            log.info("CA获取Lis签名值、用户证书接口定时任务获取到未处理的业务数据条数：{}=================", caSignDataList.size());
            String returnStr = "";
            String url = "http://10.10.2.54/gateway/recipe/synSignDataSearch";
            for(CaSignDataLis ca : caSignDataList) {
                String params = "{\n" +
                        "    \"head\": {\n" +
                        "        \"clientId\": \"2020092910330581\", \n" +
                        "        \"clientSecret\": \"2020092910330570\"\n" +
                        "    }, \n" +
                        "    \"body\": {\n" +
                        "        \"uniqueId\": \"{{uniqueId}}\", \n" +
                        "        \"isP1\": \"true\"\n" +
                        "    }\n" +
                        "}";
                //Lis
                params = params.replace("{{uniqueId}}", ca.getCaId());
                log.info("请求信息：url={}, param={}", url, params);
                returnStr = HttpUtils.doPost(url, params);
                log.info("请求信息：返回值={}", returnStr);
                if(StringUtils.isNotBlank(returnStr)) {
                    JSONObject jsonObject = new JSONObject(returnStr);
                    //获取成功
                    if(jsonObject.getInt("status") == 0) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        if(!Objects.isNull(data)) {
                            log.info("唯一值{}的签名值：{}", ca.getCaId(), data.getStr("p1"));
                            log.info("唯一值{}的证书值：{}", ca.getCaId(), data.getStr("cert"));
                            //更新业务状态及业务数据
                            ca.setQmnr(data.getStr("p1"));
                            ca.setCert(data.getStr("cert"));
                            caSignDataService.updateById(ca);
                        }
                    }
                }
            }
        }
    }
}